package com.tourlaid.mamagerment.service.impl

import com.tourlaid.mamagerment.dto.StockOutDto
import com.tourlaid.mamagerment.entity.StockOut
import com.tourlaid.mamagerment.enums.TldExceptionEnum
import com.tourlaid.mamagerment.mapper.StockOutMapper
import com.tourlaid.mamagerment.service.StockInService
import com.tourlaid.mamagerment.service.StockOutService
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.util.TldException
import org.springframework.stereotype.Service
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/23.
 */
@Service("stockOutService")
class StockOutServiceImpl : StockOutService {

    @Resource
    var stockInService: StockInService? = null
    @Resource
    var stockOutMapper: StockOutMapper? = null

    override fun queryForList(shopId: Long?, goodsId: Long?): List<StockOutDto>? {
        return stockOutMapper!!.queryForList(shopId, goodsId)
    }

    override fun insert(controlNo: String?, shopId: Long?, stockInId: Long?, stock: Int?) {
        //防止两个线程同时为一个入库单产生出库单
        synchronized(this) {
            controlNo ?: throw TldException(TldExceptionEnum.CONTROLNO_NULL)
            shopId ?: throw TldException(TldExceptionEnum.SHOPID_NULL)
            stockInId ?: throw TldException(TldExceptionEnum.STOCKINID_NULL)
            stock ?: throw TldException(TldExceptionEnum.STOCK_NULL)
            if (0.compareTo(stock) >= 0) throw TldException(TldExceptionEnum.STOCK_TOO_SMALL)
            var stockIn = stockInService!!.getById(stockInId)
            if (stockIn == null) throw TldException(TldExceptionEnum.STOCKIN_NULL)
            if (stockIn.stock!!.compareTo(stock) < 0) throw TldException(TldExceptionEnum.STOCK_TOO_BIG)
            var stockOutExist = this.getControlNo(controlNo)
            if (stockOutExist != null) throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
            var stockOut = StockOut()
            stockOut.controlNo = controlNo
            stockOut.shopId = shopId
            stockOut.stockInId = stockInId
            stockOut.stock = stock
            stockOut.creator = LoginUtil.userId
            stockOut.editor = LoginUtil.userId
            stockOutMapper!!.insert(stockOut)
        }
    }

    override fun getById(id: Long): StockOut? {
        return stockOutMapper!!.getById(id)
    }

    override fun update(stockOutId: Long?, shopId: Long?, controlNo: String?) {
        stockOutId ?: throw TldException(TldExceptionEnum.ID_NULL)
        controlNo ?: throw TldException(TldExceptionEnum.CONTROLNO_NULL)
        shopId ?: throw TldException(TldExceptionEnum.SHOPID_NULL)
        val stockOut = this.getControlNo(controlNo);
        if (stockOut != null && !Objects.equals(stockOutId, stockOut.id)) {
            throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
        }
        var newStockIn = StockOut()
        newStockIn.id = stockOutId
        newStockIn.shopId = shopId
        newStockIn.controlNo = controlNo
        newStockIn.editor = LoginUtil.userId
        stockOutMapper!!.update(newStockIn)
    }

    override fun delete(id: Long?) {
        stockOutMapper!!.delete(id, LoginUtil.userId)
    }

    override fun getControlNo(controlNo: String): StockOut? {
        return stockOutMapper!!.getControlNo(controlNo)
    }


}